#ifndef SPEEDOMETER_H
#define SPEEDOMETER_H

#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <qmath.h>


namespace Ui {
class Speedometer;
}

//int range_max = 200;
//int range_min = 0;
//int scale_major = 20;
//int scale_minor = 2;//主刻度的分划数量
class QMeter : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int value READ value WRITE setValue)
    Q_PROPERTY(int rangeMax READ rangeMax WRITE setRangeMax)
    Q_PROPERTY(int rangeMin READ rangeMin WRITE setRangeMin)
    Q_PROPERTY(int scaleMajor READ scaleMajor WRITE setScaleMajor)
    Q_PROPERTY(int scaleMinor READ scaleMinor WRITE setScaleMinor)
    Q_PROPERTY(int scaleText READ scaleText WRITE setScaleText)
    Q_PROPERTY(int radius READ radius WRITE setRadius)
    Q_PROPERTY(qreal angleRange READ angleRange WRITE setAngleRange)
    Q_PROPERTY(QString meterName READ meterName WRITE setMeterName)
    Q_PROPERTY(bool autoResize READ autoResize WRITE setAutoResize)
    Q_PROPERTY(QFont textFont READ TextFont WRITE setTextFont)

public:
    explicit QMeter(QWidget *parent = NULL);
    ~QMeter();
    int value() const;
    int rangeMax() const;
    int rangeMin() const;
    int scaleMajor() const;
    int scaleMinor() const;
    qreal angleRange() const;
    int scaleText() const;
    QString meterName() const;

    int radius() const;

    bool autoResize() const;

    QFont TextFont() const;

public slots:
    void setValue(int val);
    void setRangeMax(int rangeMax);
    void setRangeMin(int rangeMin);
    void setScaleMajor(int scaleMajor);
    void setScaleMinor(int scaleMinor);
    void setAngleRange(qreal angleRange);
    void setScaleText(int scaleText);
    void setMeterName(QString meterName);
    void setRadius(int radius);

    void setAutoResize(bool autoResize);

    void setTextFont(QFont textFont);

protected:
    void paintEvent(QPaintEvent*);

private:
    Ui::Speedometer *ui;
    int _width;
    int _height;

    void drawBorder(QPainter* painter);
    void drawBg(QPainter* painter);//绘制背景
    void drawScale(QPainter* painter);//绘制刻度
    void drawPointer(QPainter* painter);//绘制指针
    void drawMeterName(QPainter* painter);//绘制表名

    QPoint CustomRotate(QPointF point, qreal rotate);
    QPointF polarCoord(qreal r, qreal ang);
    int m_value;
    int m_rangeMax;
    int m_rangeMin;
    int m_scaleMajor;
    int m_scaleMinor;
    qreal m_angleRange;
    int m_scaleText;
    QString m_meterName;
    int m_radius;
    bool m_autoResize;
    QFont m_textFont;
};



#endif // SPEEDOMETER_H
